# -*- coding: utf-8 -*-
#
# Copyright 2009 Vanderbilt University
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 


from decorator import decorator
from inspect import getargspec, isfunction, ismethod
from hornet.event import *



@simple_decorator
def check2(func):
    def new(*args, **kw):
        print args
        print 'checked twice'
        return func(*args, **kw)
    return new

#@decorator
#def check(func, *args, **kw):
#    #print args
#    print 'checked'
#    return func(*args, **kw)



class Plugin(object):
    def accepted_events(self):
        return GraphsReady

    def __new__(cls):
        obj = object.__new__(cls)
        #cls.notify = check(cls.notify)
        #obj.notify = check(obj.notify)
        #def flatten(*args):
        #    return obj.notify(*args)
        #cls.notify = check(flatten)
        
        #print getargspec(Plugin.notify)
        return obj
    
    def __init__(self):
        #self.notify = check(self.notify)
        #print '1 ' +  
        pass
    
    @check2
    def notify(self, event):
        print event
    
    def decor(self, func):
        def new(event):
            if is_accepted_event(event, self.accepted_events()):
                return func(event)
            else:
                raise EventNotAccepted, '%s not accepted by plugin, %s' % (event, self.id)
        new.__name__ = func.__name__
        new.__doc__ = func.__doc__
        new.__module__ = func.__module__
        new.__dict__.update(func.__dict__)
        return new
 
        

class SubPlugin(Plugin):
    def notify(self, event):
        print 'this is the sub'    
    
if __name__ == '__main__':
    p = Plugin()
    #p = SubPlugin()
    #print '%s %s' % (isfunction(p.notify), ismethod(p.notify))
    #print '%s %s' % (isfunction(Plugin.notify), ismethod(Plugin.notify))
    #print ''
    #check(Plugin.notify)
    #print getargspec(Plugin.notify)
    #print getargspec(p.notify)
    #print ''
    p.notify(GraphsReady('data', caller = 'sumo'))
